package com.dingwen.treasure.auth.feign.fallback;

import cn.hutool.core.util.StrUtil;
import com.dingwen.treasure.auth.feign.TreasureManageFeignClient;
import com.dingwen.treasure.base.constant.ServerConstant;
import com.dingwen.treasure.base.pojo.vo.Result;
import com.dingwen.treasure.base.pojo.vo.ResultGenerator;
import com.dingwen.treasure.model.entity.mybatisplus.SysElement;
import com.dingwen.treasure.model.entity.mybatisplus.SysMenu;
import com.dingwen.treasure.model.entity.mybatisplus.SysUser;
import com.dingwen.treasure.model.vo.SysMenuVo;
import feign.hystrix.FallbackFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 　TreasureManageFeignClientFallback: auth ---> manage hystrix 的兜底方法
 * 　@author dingwen
 * 　@date 2022/6/19
 */
@Component
@Slf4j
public class TreasureManageFeignClientFallback implements FallbackFactory<TreasureManageFeignClient> {

    /**
     * throwable
     */
    private Throwable throwable;

    private final TreasureManageFeignClient treasureManageFeignClient = new TreasureManageFeignClient() {

        /**
         * 按帐户获取一个用户信息
         *
         * @param account 账户
         * @return {@link Result}<{@link SysUser}>
         */
        @Override
        public Result<SysUser> getOneByAccount(String account) {
            String message = StrUtil.format("{}通过feign调用{},出现错误，方法{},账号{},异常信息{}",
                    ServerConstant.TREASURE_AUTH, ServerConstant.TREASURE_MANAGE, "getOneByAccount",
                    account, throwable.getMessage());
            log.error(message);
            return ResultGenerator.genFailureResult(message);
        }

        /**
         * 按用户id获取系统菜单
         *
         * @param userId 用户id
         * @return {@link Result}<{@link List}<{@link SysMenu}>>
         */
        @Override
        public Result<List<SysMenu>> getSysMenusByUserId(Long userId) {
            String message = StrUtil.format("{}通过feign调用{},出现错误，方法{},账号{},异常信息{}",
                    ServerConstant.TREASURE_AUTH, ServerConstant.TREASURE_MANAGE, "getSysMenusByUserId",
                    userId, throwable.getMessage());
            log.error(message);
            return ResultGenerator.genFailureResult(message);
        }

        /**
         * 按用户id获取系统元素
         *
         * @param userId 用户id
         * @return {@link Result}<{@link List}<{@link SysElement}>>
         */
        @Override
        public Result<List<SysElement>> getSysElementsByUserId(Long userId) {
            String message = StrUtil.format("{}通过feign调用{},出现错误，方法{},账号{},异常信息{}",
                    ServerConstant.TREASURE_AUTH, ServerConstant.TREASURE_MANAGE, "getSysElementsByUserId",
                    userId, throwable.getMessage());
            log.error(message);
            return ResultGenerator.genFailureResult(message);
        }

        /**
         * 获取树系统菜单
         *
         * @param userId 用户id
         * @return {@link Result}<{@link List}<{@link SysMenuVo}>>
         */
        @Override
        public Result<List<SysMenuVo>> getSysMenusForTree(Long userId) {
            String message = StrUtil.format("{}通过feign调用{},出现错误，方法{},账号{},异常信息{}",
                    ServerConstant.TREASURE_AUTH, ServerConstant.TREASURE_MANAGE, "getSysMenusForTree",
                    userId, throwable.getMessage());
            log.error(message);
            return ResultGenerator.genFailureResult(message);
        }
    };

    @Override
    public TreasureManageFeignClient create(Throwable throwable) {
        this.throwable = throwable;
        return treasureManageFeignClient;
    }
}
